package cn.iocoder.yudao.module.of.service.meetingminutes;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import cn.iocoder.yudao.module.of.controller.admin.meetingminutesenduser.vo.MeetingMinuteSendUserRespVO;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingminutesenduser.MeetingMinuteSendUserDO;
import cn.iocoder.yudao.module.of.dal.dataobject.taskinfo.TaskInfoDO;
import cn.iocoder.yudao.module.of.dal.mysql.meetingminutesenduser.MeetingMinuteSendUserMapper;
import cn.iocoder.yudao.module.of.dal.mysql.taskinfo.TaskInfoMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.of.controller.admin.meetingminutes.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingminutes.MeetingMinutesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.of.dal.mysql.meetingminutes.MeetingMinutesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.of.enums.ErrorCodeConstants.*;

/**
 * 会议纪要 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MeetingMinutesServiceImpl implements MeetingMinutesService {

    @Resource
    private MeetingMinutesMapper meetingMinutesMapper;
    @Resource
    private FileMapper fileMapper;
    @Resource
    private AdminUserMapper userMapper;
    @Resource
    private MeetingMinuteSendUserMapper sendUserMapper;
    @Resource
    private TaskInfoMapper taskInfoMapper;

    @Override
    public Long createMeetingMinutes(MeetingMinutesSaveReqVO createReqVO) {
        // 插入
        MeetingMinutesDO meetingMinutes = BeanUtils.toBean(createReqVO, MeetingMinutesDO.class);
        //meetingMinutes.setCreatorName(WebFrameworkUtils.getLoginUserId())
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        AdminUserDO adminUserDO = userMapper.selectById(loginUserId);
        if(adminUserDO != null){
            meetingMinutes.setCreatorName(adminUserDO.getNickname());
        }
        meetingMinutesMapper.insert(meetingMinutes);
        // 返回
        return meetingMinutes.getId();
    }

    @Override
    public void updateMeetingMinutes(MeetingMinutesSaveReqVO updateReqVO) {
        // 校验存在
        validateMeetingMinutesExists(updateReqVO.getId());
        // 更新
        MeetingMinutesDO updateObj = BeanUtils.toBean(updateReqVO, MeetingMinutesDO.class);
        meetingMinutesMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteMeetingMinutes(Long id) {
        // 校验存在
        validateMeetingMinutesExists(id);
        validateTaskExists(id);
        //删除纪要之前，先删除纪要下派人
        sendUserMapper.delete(MeetingMinuteSendUserDO::getMinuteId,id);

        // 删除
        meetingMinutesMapper.deleteById(id);
    }

    private void validateTaskExists(Long id) {
        List<TaskInfoDO> taskInfoDOS = taskInfoMapper.selectList(TaskInfoDO::getMeetingId, id);
        if(!taskInfoDOS.isEmpty()){
            throw exception(MEETING_MINUTES_TASK_EXISTS);
        }


    }

    private void validateMeetingMinutesExists(Long id) {
        if (meetingMinutesMapper.selectById(id) == null) {
            throw exception(MEETING_MINUTES_NOT_EXISTS);
        }
    }

    @Override
    public MeetingMinutesDO getMeetingMinutes(Long id) {
        return meetingMinutesMapper.selectById(id);
    }

    @Override
    public PageResult<MeetingMinutesDO> getMeetingMinutesPage(MeetingMinutesPageReqVO pageReqVO) {
        return meetingMinutesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MeetingMinutesRespVO> getMeetingMinutesList(MeetingMinutesPageReqVO pageReqVO) {
        LambdaQueryWrapperX<MeetingMinutesDO> wrapper = new LambdaQueryWrapperX<MeetingMinutesDO>()
                .eqIfPresent(MeetingMinutesDO::getMeetingId, pageReqVO.getMeetingId())
                .eqIfPresent(MeetingMinutesDO::getMainRespDeptId, pageReqVO.getMainRespDeptId())
                .likeIfPresent(MeetingMinutesDO::getMainRespDeptName, pageReqVO.getMainRespDeptName())
                .eqIfPresent(MeetingMinutesDO::getMainRespUserId, pageReqVO.getMainRespUserId())
                .eqIfPresent(MeetingMinutesDO::getTaskGenUserId, pageReqVO.getTaskGenUserId())
                .likeIfPresent(MeetingMinutesDO::getTaskGenUserName, pageReqVO.getTaskGenUserName())
                .betweenIfPresent(MeetingMinutesDO::getCreateTime, pageReqVO.getCreateTime())
                .orderByDesc(MeetingMinutesDO::getId);
        List<MeetingMinutesDO> meetingMinutesDOS = meetingMinutesMapper.selectList(wrapper);
        List<MeetingMinutesRespVO> list = meetingMinutesDOS.stream().map(meeting -> {
            MeetingMinutesRespVO meetingMinutesRespVO = BeanUtils.toBean(meeting, MeetingMinutesRespVO.class);
            Long minuteAttach = meeting.getMinuteAttach();
            FileDO fileDO = fileMapper.selectById(minuteAttach);
            if (fileDO != null) {
                meetingMinutesRespVO.setMinuteName(fileDO.getName());
                meetingMinutesRespVO.setFileDO(fileDO);
            }
            return meetingMinutesRespVO;
        }).collect(Collectors.toList());

        return list;
    }

    @Override
    public List<MeetingMinutesRespVO> getMinuteList(Long meetingID) {
        List<MeetingMinutesDO> meetingMinutesDOS = meetingMinutesMapper.selectList(MeetingMinutesDO::getMeetingId,meetingID);
        List<MeetingMinutesRespVO> list = meetingMinutesDOS.stream().map(meeting -> {
            MeetingMinutesRespVO meetingMinutesRespVO = BeanUtils.toBean(meeting, MeetingMinutesRespVO.class);
            Long minuteAttach = meeting.getMinuteAttach();
            FileDO fileDO = fileMapper.selectById(minuteAttach);
            if (fileDO != null) {
                meetingMinutesRespVO.setMinuteName(fileDO.getName());
              //  meetingMinutesRespVO.setFileDO(fileDO);
                meetingMinutesRespVO.setMinuteAttachFile(fileDO);
            }
            List<MeetingMinuteSendUserDO> meetingMinuteSendUserDOS = sendUserMapper.selectList(MeetingMinuteSendUserDO::getMinuteId, meeting.getId(),
                    MeetingMinuteSendUserDO::getDeleted,Boolean.FALSE);
          //  List<MeetingMinuteSendUserRespVO> meetingMinutesSaveReqVOS = BeanUtils.toBean(meetingMinuteSendUserDOS, MeetingMinuteSendUserRespVO.class);
            List<MeetingMinuteSendUserRespVO> meetingMinutesSaveReqVOS =  meetingMinuteSendUserDOS.stream().map(msu->{
                MeetingMinuteSendUserRespVO meetingMinuteSendUserRespVO = BeanUtils.toBean(msu, MeetingMinuteSendUserRespVO.class);
                AdminUserDO adminUserDO = userMapper.selectById(msu.getCreator());
                if(adminUserDO!= null){
                    meetingMinuteSendUserRespVO.setCreatorName(adminUserDO.getNickname());
                    meetingMinutesRespVO.setMinuteSendUserName(adminUserDO.getNickname());

                }
                meetingMinutesRespVO.setMinuteSendTime(msu.getCreateTime());
                return meetingMinuteSendUserRespVO;
            }).collect(Collectors.toList());

            meetingMinutesRespVO.setSendUserList(meetingMinutesSaveReqVOS);


            return meetingMinutesRespVO;
        }).collect(Collectors.toList());
        return list;
    }

}